package cn.xsly.system.controller;

import cn.xsly.basic.util.PageList;
import cn.xsly.basic.util.AjaxResult;
import cn.xsly.system.domain.DicType;
import cn.xsly.system.query.DicTypeQuery;
import cn.xsly.system.service.DicTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@RequestMapping("/systemdictionarytype")
public class DicTypeController {
    @Autowired


    private DicTypeService typeService;

    /*查询所有*/
    @GetMapping
    public List<DicType> findAll() {
        return typeService.findAll();
    }

    /*查询单个*/
    @GetMapping("/{id}")
    public DicType findOne(@PathVariable("id") Long id) {
        return typeService.findById(id);
    }

    /*分页查询*/
    @PostMapping
    public PageList<DicType> queryPageList(@RequestBody DicTypeQuery typeQuery) {
        return typeService.queryPage(typeQuery);
    }

    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {


        try {
            typeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "失败");
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DicType type){
        try {
            if(type.getId()==null){
                typeService.add(type);
            }else {
                typeService.update(type);
            }
            /*成功返回静态值*/
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败了");
        }
    }

    @PatchMapping
    @ApiOperation(value="批量删除接口")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            typeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
}
